package com.yy.personalcv.controller;

import com.yy.personalcv.pojo.Project;
import com.yy.personalcv.pojo.UserInformation;
import com.yy.personalcv.service.ProjectService;
import com.yy.personalcv.tool.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @RequestMapping(value = "/projectList.do")
    public String getProjectList(HttpServletRequest request, Model model,
                                 @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "5") int size){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        int counts = getProjectCounts(uid);
        List<Project> projects = selectTen(page, size, uid);
        model.addAttribute("projects", projects);
        model.addAttribute("page", page);
        model.addAttribute("size", projects.size());
        model.addAttribute("max_size", size);
        model.addAttribute("counts", counts);
        model.addAttribute("userInformation", userInformation);
        return "project_page";
    }

    @RequestMapping(value = "/projectDetail.do")
    public String getSkillDetail(HttpServletRequest request, Model model,
                                 @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        Project project = getProjectDetail(id, uid);
        if(project == null){
            return "redirect:/projectList";
        }
        model.addAttribute("project", project);
        model.addAttribute("userInformation", userInformation);
        return "project_detail_page";
    }

    @RequestMapping(value = "/addProject.do")
    public String addSkill(HttpServletRequest request, Model model,
                           @RequestParam String name, @RequestParam String language, @RequestParam String url,@RequestParam String description){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(name)
                || StringUtils.getInstance().isNullOrEmpty(description)
                || StringUtils.getInstance().isNullOrEmpty(language)
                || StringUtils.getInstance().isNullOrEmpty(url)){
            log.error("one of them is null");
            return "redirect:/projectList.do";
        }
        int uid = userInformation.getId();
        Project project = new Project();
        project.setName(name);
        project.setLanguage(language);
        project.setUrl(url);
        project.setDescription(description);
        project.setUid(uid);
        projectService.addProject(project);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/projectList.do";
    }

    @RequestMapping(value = "/projectDelete.do")
    public String deleteSkill(HttpServletRequest request, Model model,
                              @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(id)){
            log.error("id is null");
            return "redirect:/skillList.do";
        }
        int uid = userInformation.getId();
        projectService.deleteProject(id, uid);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/projectList.do";
    }

    @RequestMapping(value = "/projectUpdate.do", method = RequestMethod.POST)
    public String updateSkill(HttpServletRequest request, Model model,
                              @RequestParam int id, @RequestParam String name, @RequestParam String language, @RequestParam String url,@RequestParam String description){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(id)
                || StringUtils.getInstance().isNullOrEmpty(name)
                || StringUtils.getInstance().isNullOrEmpty(description)
                || StringUtils.getInstance().isNullOrEmpty(language)
                || StringUtils.getInstance().isNullOrEmpty(url)){
            log.error("one of them is null");
            return "redirect:/projectList.do";
        }
        int uid = userInformation.getId();
        Project project = new Project();
        project.setId(id);
        project.setName(name);
        project.setLanguage(language);
        project.setUrl(url);
        project.setDescription(description);
        project.setUid(uid);
        projectService.updateProject(project);
        Project project1 = getProjectDetail(id, uid);
        model.addAttribute("project", project1);
        model.addAttribute("userInformation", userInformation);
        return "project_detail_page";
    }

    private List<Project> selectTen(int page, int size, int uid){
        Map map = new HashMap<>();
        map.put("start", (page-1)*size);
        map.put("size", size);
        map.put("uid", uid);
        return projectService.selectTen(map);
    }

    private int getProjectCounts(int uid){
        return projectService.getProjectCounts(uid);
    }

    private Project getProjectDetail(int id, int uid){
        return projectService.getProjectDetail(id, uid);
    }


}
